package cn.edu.sdjzu.service.impl;

import cn.edu.sdjzu.dao.UserDao;
import cn.edu.sdjzu.entity.User;
import cn.edu.sdjzu.exception.BaseException;
import cn.edu.sdjzu.factory.DaoFactory;
import cn.edu.sdjzu.service.UserService;
import cn.edu.sdjzu.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;

public final class UserServiceImpl implements UserService {
    //DaoFactory.getDao方法声明了BaseException，但后者扩展了RuntimeException，
    //调用者不再强制处理
    private UserDao userDao =
            DaoFactory.getDao(UserDao.class, "UserDaoImpl");
    @Override
    public void login(User userToLogin) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        userDao.login(connection, userToLogin);
        JdbcHelper.close(connection);
    }



    //  不会查找一个User集合
    //	不会增加一个User
    //	不会修改一个User
    //	不会删除一个User
    @Override
    public User find(Integer id) throws SQLException, BaseException {
        Connection connection = JdbcHelper.getConn();
//        User actor = actorDao.find(connection, id);
        User user = userDao.find(connection, id);
        JdbcHelper.close(connection);
        return user;
    }

    //这里修改user, 是不是就不用返回了，调用者使用引用
    @Override
    public int add(User user)throws SQLException {
        Connection connection = JdbcHelper.getConn();
        int id = userDao.add(connection, user);
        JdbcHelper.close(connection);
        return id;
    }



//    public Actor login(User userToLogin) throws SQLException {
//        Connection connection = JdbcHelper.getConn();
//        Actor actor = userDao.login(connection, userToLogin);
//        JdbcHelper.close(connection);
//        return actor;
//    }


}
